#include "sqlitepool.h"

SQLitePool* SQLitePool::m_Instance = nullptr;
QMutex SQLitePool::m_mutex;

SQLitePool *SQLitePool::getInstance(QObject *parent)
{
    // 双重检查锁定，保证线程安全且高效
    if (!m_Instance) {
        QMutexLocker locker(&m_mutex); // 自动加锁/解锁，避免死锁
        if (!m_Instance) {
            m_Instance = new SQLitePool(parent);
        }
    }
    return m_Instance;
}

void SQLitePool::reset_Order_Process_List_Database()
{
    if(m_Order_Process_List_Database)
    {
        m_Order_Process_List_Database->close();

        m_Order_Process_List_Database->removeDatabase("Order_Process_List_DataBase");

        delete m_Order_Process_List_Database;

        m_Order_Process_List_Database = nullptr;
    }

    m_Order_Process_List_Database = new QSqlDatabase();

    *m_Order_Process_List_Database = QSqlDatabase::addDatabase("QSQLITE",QString("Order_Process_List_DataBase"));

    m_Order_Process_List_Database->setDatabaseName("./Order_Process_List.db");
    if(!m_Order_Process_List_Database->open())
    {
        qDebug()<<"Cpp:m_Order_Process_List_Database->lastError():"<<m_Order_Process_List_Database->lastError().text().toUtf8().data();
    }
    else
    {
        qDebug()<<"Cpp:连接数据库 : Order_Process_List.db 成功";
    }

    qDebug()<<"Cpp: 重置数据库  Order_Process_List.db 成功";
}

QSqlDatabase *SQLitePool::get_Order_Process_List_DataBase()
{
    return m_Order_Process_List_Database;
}

SQLitePool::SQLitePool(QObject *parent)
    : QObject{parent}
{
    reset_Order_Process_List_Database();
}

SQLitePool::~SQLitePool()
{

}
